package idiet.wizards.page;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import swing2swt.layout.BorderLayout;

public class WizardPageMealRecipe extends WizardPage {
	private final static Logger logger=Logger.getLogger(WizardPageMealRecipe.class);
	private Text textFoodName;
	private Combo comboParentCategory;
	private Combo comboCategory;
	/*private FoodNamesProvider foodNamesProvider=new FoodNamesProvider();
	private FoodNameLabelProvider foodNameLabelProvider=new FoodNameLabelProvider();
	private ChoosedFoodNamesProvider choosedFoodNamesProvider=new ChoosedFoodNamesProvider();
	private ChoosedFoodNameLabelProvider choosedFoodNameLabelProvider=new ChoosedFoodNameLabelProvider();*/
	//private FoodCategoryService foodCategoryService=(FoodCategoryService)Activator.getApplicationContext().getBean(FoodCategoryService.class);
	//private FoodService foodService=(FoodService)Activator.getApplicationContext().getBean(FoodService.class);
	private List<String> foodNames=null;
	//private List<CourseRecipe> choosedFoodNames=new ArrayList<CourseRecipe>();
	private Table tableFoods;
	private TableViewer tableViewerFoodNames;
	private TableViewer tableViewerChoosedFoods;
	private Table tableChoosedFoods;
	//private CourseRecipeService courseRecipeService=(CourseRecipeService)Activator.getApplicationContext().getBean(CourseRecipeService.class);
	
	/*public List<CourseRecipe> getChoosedFoodNames() {
		return choosedFoodNames;
	}

	public void setChoosedFoodNames(List<CourseRecipe> choosedFoodNames) {
		this.choosedFoodNames = choosedFoodNames;
	}*/

	//private CourseRecipe courseRecipe;
	/**
	 * Create the wizard.
	 */
	public WizardPageMealRecipe() {
		super("wizardPage");
		setTitle("\u9009\u62E9\u98DF\u6750");
		setDescription("\u9009\u62E9\u98DF\u6750\uFF0C\u53EF\u4EE5\u901A\u8FC7\u98DF\u6750\u7C7B\u522B\u6216\u8005\u76F4\u63A5\u8F93\u5165\u98DF\u6750\u90E8\u5206\u540D\u79F0\u641C\u7D22");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		GridLayout gl_container = new GridLayout(3, false);
		gl_container.marginRight = 50;
		gl_container.marginLeft = 50;
		container.setLayout(gl_container);
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new BorderLayout(0, 0));
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 173;
		composite.setLayoutData(gd_composite);
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(BorderLayout.NORTH);
		composite_2.setLayout(new GridLayout(4, false));
		
		
		
		Label lblNewLabel = new Label(composite_2, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("\u98DF\u6750\u7C7B\u522B\uFF1A");
		
		comboParentCategory = new Combo(composite_2, SWT.NONE);
		comboParentCategory.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				/*comboCategory.setItems(foodCategoryService.getFoodSubcategories(comboParentCategory.getText()).toArray(new String[0]));
				comboCategory.select(0);
				tableViewerFoodNames.setInput(foodService.getFoodNames(comboCategory.getText()));*/
			}
		});
		GridData gd_comboParentCatrgory = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_comboParentCatrgory.horizontalIndent = 5;
		//comboParentCategory.setItems(foodCategoryService.getFoodCategories().toArray(new String[0]));
		comboParentCategory.setLayoutData(gd_comboParentCatrgory);
		comboParentCategory.select(0);
		
		Label lblNewLabel_1 = new Label(composite_2, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("食材子类：");
		
		comboCategory = new Combo(composite_2, SWT.NONE);
		comboCategory.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//tableViewerFoodNames.setInput(foodService.getFoodNames(comboCategory.getText()));
				
			}
		});
		GridData gd_comboCategory = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		//comboCategory.setItems(foodCategoryService.getFoodSubcategories(comboParentCategory.getText()).toArray(new String[0]));
		gd_comboCategory.horizontalIndent = 5;
		comboCategory.setLayoutData(gd_comboCategory);
		comboCategory.select(0);
		
		Label label = new Label(composite_2, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("\u98DF\u6750\u540D\u79F0\uFF1A");
		
		textFoodName = new Text(composite_2, SWT.BORDER);
		GridData gd_textFoodName = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textFoodName.widthHint = 99;
		gd_textFoodName.horizontalIndent = 5;
		textFoodName.setLayoutData(gd_textFoodName);
		
		Button btnSearchFood = new Button(composite_2, SWT.NONE);
		btnSearchFood.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//tableViewerFoodNames.setInput(foodService.getFoodNames4Search(textFoodName.getText()));
			}
		});
		btnSearchFood.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnSearchFood.setText("   \u641C\u7D22   ");
		
		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayoutData(BorderLayout.CENTER);
		TableColumnLayout tcl_composite_3 = new TableColumnLayout();
		composite_3.setLayout(tcl_composite_3);
		
		tableViewerFoodNames = new TableViewer(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		tableViewerFoodNames.setUseHashlookup(true);
		tableViewerFoodNames.setContentProvider(new ArrayContentProvider());
		//tableViewerFoodNames.setLabelProvider(foodNameLabelProvider);
		//foodNames=foodService.getFoodNames(comboCategory.getText());
		tableViewerFoodNames.setInput(foodNames);
		
		tableFoods = tableViewerFoodNames.getTable();
		tableFoods.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				TableItem[] selection =tableFoods.getSelection();
				for(TableItem item:selection){
					if(logger.isDebugEnabled()){
						logger.debug("select : "+item.getText(0));
					}
				}
				//insertFoodNamesIntoChoosedTable();
			}
		});
		tableFoods.setHeaderVisible(true);
		tableFoods.setLinesVisible(true);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewerFoodNames, SWT.NONE);
		TableColumn tblclmnFoodName = tableViewerColumn.getColumn();
		tcl_composite_3.setColumnData(tblclmnFoodName, new ColumnPixelData(150, true, true));
		tblclmnFoodName.setText("食物");	
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return (String)element;
			}
			
		});
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Button btnChoose = new Button(composite_1, SWT.NONE);
		btnChoose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//insertFoodNamesIntoChoosedTable();
			}
		});
		btnChoose.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, true, 1, 1));
		btnChoose.setSize(80, 27);
		btnChoose.setText("   >  ");
		
		Button btnUnchoose = new Button(composite_1, SWT.NONE);
		btnUnchoose.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e){
				int[] selection=tableChoosedFoods.getSelectionIndices();
				for(int i=0;i<selection.length;i++){
					String itemValue=tableChoosedFoods.getItem(selection[i]).getText(0);
					TableItem tableItem=new TableItem(tableFoods,SWT.NONE);
					tableItem.setText(itemValue);
				}
				tableChoosedFoods.remove(selection);
			}
		});
		btnUnchoose.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
		btnUnchoose.setText("   <  ");
		
		Composite composite_4 = new Composite(container, SWT.NONE);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		TableColumnLayout tcl_composite_4 = new TableColumnLayout();
		composite_4.setLayout(tcl_composite_4);
		
		
		//foodNames=foodService.getFoodNames(comboCategory.getText());
		
		
		tableViewerChoosedFoods = new TableViewer(composite_4, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		tableChoosedFoods = tableViewerChoosedFoods.getTable();
		tableChoosedFoods.setHeaderVisible(true);
		tableChoosedFoods.setLinesVisible(true);
		
		TableViewerColumn tableViewerColumnFoodName = new TableViewerColumn(tableViewerChoosedFoods, SWT.NONE);
		TableColumn tblclmnChoosedFoodName = tableViewerColumnFoodName.getColumn();
		tcl_composite_4.setColumnData(tblclmnChoosedFoodName, new ColumnPixelData(150, true, true));
		tblclmnChoosedFoodName.setText("食材");
		tableViewerColumnFoodName.setLabelProvider(new CellLabelProvider(){

			@Override
			public void update(ViewerCell cell) {
				// TODO Auto-generated method stub
				//cell.setText(((CourseRecipe) cell.getElement()).getFoodId());
				
			}
			/*@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((CourseRecipe)element).getFoodName();
			}*/
			
		});
		//tableViewerColumn_1.setEditingSupport(new TableViewerChoosedFoodsEditingSupport(tableViewerFoodNames, 0));
		tableViewerColumnFoodName.setEditingSupport(new EditingSupport(tableViewerChoosedFoods){

			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				return new TextCellEditor(tableViewerChoosedFoods.getTable());
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				return null;
				//return ((CourseRecipe)element).getFoodId();
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				//((CourseRecipe)element).setFoodId(String.valueOf(value));
				tableViewerChoosedFoods.refresh();
			}
			
		});
		TableViewerColumn tableViewerColumnQuantity = new TableViewerColumn(tableViewerChoosedFoods, SWT.NONE);
		TableColumn tblclmnChoosedQuantity = tableViewerColumnQuantity.getColumn();
		tcl_composite_4.setColumnData(tblclmnChoosedQuantity, new ColumnPixelData(150, true, true));
		tblclmnChoosedQuantity.setText("份量");
		tableViewerColumnQuantity.setLabelProvider(new CellLabelProvider(){
			@Override
			public void update(ViewerCell cell) {
				// TODO Auto-generated method stub
				//cell.setText(String.valueOf(new Float(((CourseRecipe) cell.getElement()).getQuantity())));
				
			}

			/*@Override
			public String getText(Object element){
				return (new Float((((CourseRecipe)element).getQuantity()))).toString();
			}*/
		});
		tableViewerColumnQuantity.setEditingSupport(new EditingSupport(tableViewerChoosedFoods){

			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				return null;
				//return new NumberCellEditor(tableViewerChoosedFoods.getTable());
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				return null;
				//return ((CourseRecipe)element).getQuantity();
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("new value is "+value);
				}
				//((CourseRecipe)element).setQuantity(new Float(value.toString()));
				tableViewerChoosedFoods.refresh(element);
			}
			
		});
		
		TableViewerColumn tableViewerColumnUnit = new TableViewerColumn(tableViewerChoosedFoods, SWT.NONE);
		TableColumn tblclmnChoosedUnit = tableViewerColumnUnit.getColumn();
		tcl_composite_4.setColumnData(tblclmnChoosedUnit, new ColumnPixelData(150, true, true));
		tblclmnChoosedUnit.setText("单位");
		tableViewerColumnUnit.setLabelProvider(new ColumnLabelProvider(){
			/*@Override
			public String getText(Object element){
				//return ((CourseRecipe)element).getUnit();
			}*/
		});
		tableViewerColumnUnit.setEditingSupport(new EditingSupport(tableViewerChoosedFoods){
			//private List<String> units=courseRecipeService.getCourseRecipeMaterialUnit();
			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				return null;
				//return new ComboBoxCellEditor(tableViewerChoosedFoods.getTable(),units.toArray(new String[0]));
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				return null;
				//return units.indexOf(((CourseRecipe)element).getUnit());
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("new value is "+value);
				}
				//((CourseRecipe)element).setUnit(units.get((Integer) value));
				tableViewerChoosedFoods.refresh();
			}
			
		});
		tableViewerChoosedFoods.setContentProvider(new ArrayContentProvider());
		//tableViewerChoosedFoods.setInput(choosedFoodNames);
	}
	/*private void importFoodNamesIntoTable(Table table,List<String> data){
		table.removeAll();
		if(data!=null){
			Iterator<String> it=data.iterator();
			while(it.hasNext()){
				TableItem item=new TableItem(table,SWT.NONE);
				item.setText(it.next());
			}
		}
	}*/
	/*private void insertFoodNamesIntoChoosedTable(){
		IStructuredSelection selection =(IStructuredSelection)tableViewerFoodNames.getSelection();
		for(Iterator itr=selection.iterator();itr.hasNext();){
			CourseRecipe recipe=new CourseRecipe();
			String foodName=(String)itr.next();
			foodNames.remove(foodName);
			//tableViewerFoodNames.refresh();
			recipe.setFoodId(foodName);
			recipe.setQuantity(100);
			recipe.setUnit("克");
			choosedFoodNames.add(recipe);
			tableViewerFoodNames.remove(foodName);
		}
		tableViewerChoosedFoods.refresh();
	}*/
}
